package com.h2cloud.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试循环行标志
 * @author: wulongkai
 * @create: 2023-02-09 21:07
 **/
public class test1 {
    public static void main(String[] args) {
        String templateContent =
                "<table style=\"border-collapse: collapse; width: 100%; height: 232px;\" border=\"1\">\n" +
                        "<tbody>\n" +
                        "<tr style=\"height: 188px;\">\n" +
                        "<td style=\"width: 100%; height: 188px;\">\n" +
                        "<p>&nbsp;</p>\n" +
                        "<p>&nbsp;</p>\n" +
                        "<p>&nbsp;</p>\n" +
                        "<table style=\"border-collapse: collapse; width: 100%;\" border=\"1\">\n" +
                        "<tbody>\n" +
                        "<tr>\n" +
                        "<td style=\"width: 50%;\">姓名</td>\n" +
                        "<td style=\"width: 50%;\">性别</td>\n" +
                        "</tr>\n" +
                        "<tr>\n" +
                        "<td class=\"loop-row\" style=\"width: 50%;\"><a class=\"ehrTempHighlight\" style=\"color: #000; pointer-events: none; text-decoration: none;\">#员工姓名#</a></td>\n" +
                        "<td style=\"width: 50%;\"><a class=\"ehrTempHighlight\" style=\"color: #000; pointer-events: none; text-decoration: none;\">#员工性别#</a></td>\n" +
                        "</tr>\n" +
                        "</tbody>\n" +
                        "</table>\n" +
                        "<p>&nbsp;</p>\n" +
                        "<p>&nbsp;</p>\n" +
                        "<table style=\"border-collapse: collapse; width: 100%; height: 44px;\" border=\"1\">\n" +
                        "<tbody>\n" +
                        "<tr style=\"height: 22px;\">\n" +
                        "<td style=\"width: 33.3333%; height: 22px;\">姓名</td>\n" +
                        "<td style=\"width: 33.3333%; height: 22px;\">生日</td>\n" +
                        "</tr>\n" +
                        "<tr class=\"loop-row\" style=\"height: 22px;\">\n" +
                        "<td style=\"width: 33.3333%; height: 22px;\"><a class=\"ehrTempHighlight\" style=\"color: #000; pointer-events: none; text-decoration: none;\">#员工姓名#</a></td>\n" +
                        "<td style=\"width: 33.3333%; height: 22px;\"><a class=\"ehrTempHighlight\" style=\"color: #000; pointer-events: none; text-decoration: none;\">#员工生日#</a></td>\n" +
                        "</tr>\n" +
                        "</tbody>\n" +
                        "</table>\n" +
                        "<p>&nbsp;</p>\n" +
                        "<p>&nbsp;</p>\n" +
                        "<p>&nbsp;</p>\n" +
                        "</td>\n" +
                        "</tr>\n" +
                        "</tbody>\n" +
                        "</table>";
        List<Integer> bids = new ArrayList<>();
        bids.add(1);
        bids.add(2);
        bids.add(3);

        Document document = Jsoup.parse(templateContent);
        document.outputSettings(new Document.OutputSettings().prettyPrint(false));
        Elements trs = document.select(".loop-row");
        for (Element tr : trs) {
//            String originStr = tr.toString();
//            StringBuffer targetStr = new StringBuffer();
//            for (Integer bid : bids) {
//                String targetString = fieldReplacement(originStr, bid);
//                targetStr.append(targetString).append("\n");
//            }
//            templateContent = templateContent.replace(originStr, targetStr);
            System.out.println(tr.is("tr"));
            System.out.println(tr.toString());
            System.out.println();
        }
//        templateContent = templateContent.replaceAll("loop-row", "");
//        System.out.println(templateContent);
    }

    public static String fieldReplacement(String targetString, Integer bid) {
        Map<Integer, String> map1 = new HashMap<>();
        map1.put(1, "longkai1");
        map1.put(2, "longkai2");
        map1.put(3, "longkai3");

        Map<Integer, String> map2 = new HashMap<>();
        map2.put(1, "nan");
        map2.put(2, "nu");
        map2.put(3, "nan");

        Map<Integer, String> map3 = new HashMap<>();
        map3.put(1, "1201");
        map3.put(2, "1101");
        map3.put(3, "0803");

        targetString = targetString.replace("#员工姓名#", map1.get(bid))
                .replace("#员工性别#", map2.get(bid))
                .replace("#员工生日#", map3.get(bid));
        return targetString;
    }
}
